<?php
namespace Tbf\Util;
use Tbf\Util\EncodeUtil;
use Symfony\Component\HttpFoundation\Request;
/**
 * @deprecated
 */
class HttpUtil
{
    /**
     * 发送http响应代码
     * 能发送,并且发送成功返回true
     * 由于header已经发送而不能发送,返回false
    * */
    public static function sendResponseCode($code)
    {
        if (headers_sent()) {
            return false;
        }
        switch ($code) {
        case 100: $text = 'Continue'; break;
        case 101: $text = 'Switching Protocols'; break;
        case 200: $text = 'OK'; break;
        case 201: $text = 'Created'; break;
        case 202: $text = 'Accepted'; break;
        case 203: $text = 'Non-Authoritative Information'; break;
        case 204: $text = 'No Content'; break;
        case 205: $text = 'Reset Content'; break;
        case 206: $text = 'Partial Content'; break;
        case 300: $text = 'Multiple Choices'; break;
        case 301: $text = 'Moved Permanently'; break;
        case 302: $text = 'Moved Temporarily'; break;
        case 303: $text = 'See Other'; break;
        case 304: $text = 'Not Modified'; break;
        case 305: $text = 'Use Proxy'; break;
        case 400: $text = 'Bad Request'; break;
        case 401: $text = 'Unauthorized'; break;
        case 402: $text = 'Payment Required'; break;
        case 403: $text = 'Forbidden'; break;
        case 404: $text = 'Not Found'; break;
        case 405: $text = 'Method Not Allowed'; break;
        case 406: $text = 'Not Acceptable'; break;
        case 407: $text = 'Proxy Authentication Required'; break;
        case 408: $text = 'Request Time-out'; break;
        case 409: $text = 'Conflict'; break;
        case 410: $text = 'Gone'; break;
        case 411: $text = 'Length Required'; break;
        case 412: $text = 'Precondition Failed'; break;
        case 413: $text = 'Request Entity Too Large'; break;
        case 414: $text = 'Request-URI Too Large'; break;
        case 415: $text = 'Unsupported Media Type'; break;
        case 500: $text = 'Internal Server Error'; break;
        case 501: $text = 'Not Implemented'; break;
        case 502: $text = 'Bad Gateway'; break;
        case 503: $text = 'Service Unavailable'; break;
        case 504: $text = 'Gateway Time-out'; break;
        case 505: $text = 'HTTP Version not supported'; break;
        default:
            throw new \Exception('Unknown http status code "' . htmlentities($code) . '"');
            break;
        }
        $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
        header($protocol . ' ' . $code . ' ' . $text);

        return true;
    }
    /**
     * 获取请求返回类型
     * 一般放在'return_type'里面
     * 1.'ajax'类型:返回数据应该类似于{"status":1,"info":"success","data":1} .
     * 2.'json'类型:返回数据应该类似于{"status":"success","data":1} .
     * 2.'html'类型:返回数据应该是一个有效的html页面,应该可以单独显示.
     * 3.'part_html'类型:返回数据应该是一部分html页面.
     *
     * 按照下面顺序判断
     * 1.如果get或post里面包含一个参数叫return_type 返回它的内容
     * 2.如果请求是ajax返回'ajax'
     * 3.返回'html'
     * */
    public static function getReturnType()
    {
        $return_type = self::getValueFromHttpInput('return_type');
        if ($return_type!==null) {
            return $return_type;
        }
        if (self::isAjax()) {
            return 'ajax';
        }

        return 'html';
    }
    /**
     * 判断是否是ajax请求
     * 按照下面顺序判断
     * 1.如果get或post里面包含一个参数叫ajax,如果是0表示不是ajax,是1表示是ajax
     * 2.如果get或post里面包含一个参数叫return_type 如果是'ajax' 表示该请求是ajax,如果不是'ajax'表示该请求不是ajax
     * 3.header里面包含一个参数叫X_REQUESTED_WITH,如果是'XMLHttpRequest'表示是ajax
     * 4.不是ajax
     * */
    public static function isAjax()
    {
        $ajax = self::getValueFromHttpInput('ajax');
        //1 ajax参数
        if ($ajax==1) {
            return true;
        }
        if ($ajax!==null&&$ajax==0) {
            return false;
        }
        //2 return_type参数
        $return_type = self::getValueFromHttpInput('return_type');
        if ($return_type=='ajax') {
            return true;
        }
        if ($return_type!==null) {
            return false;
        }
        //3 header
        if ((!empty($_SERVER['HTTP_X_REQUESTED_WITH']))
            &&($_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest')){
                return true;
            }
        //4 什么也没有,不是ajax
        return false;
    }
    /**
     * json返回错误
     * */
    public static function jsonError($reason='fail')
    {
        return self::jsonReturn(array(
            'status'=>$reason,
            'data'=>null
        ));
    }
    /**
     * json返回成功
     * */
    public static function jsonSuccess($data=null)
    {
        return self::jsonReturn(array(
            'status'=>'success',
            'data'=>$data
        ));
    }
    /**
     * json返回数据
     * */
    public static function jsonReturn($data)
    {
        return EncodeUtil::jsonEncode($data);
    }
    /**
     * 从http的输入里面获取一个参数
     * 顺序如下:
     * 1.REQUEST
     * 2.GET
     * 3.POST
     * 没有找到返回null
     * */
    public static function getValueFromHttpInput($key)
    {
        if (isset($_REQUEST[$key])) {
            return $_REQUEST[$key];
        }
        if (isset($_GET[$key])) {
            return $_GET[$key];
        }
        if (isset($_POST[$key])) {
            return $_POST[$key];
        }

        return null;
    }
    /**
    * 从全局变量生成symfony的request对象
    * 可以cache提高速度
    */
    public static function symfonyRequest()
    {
        return Request::createFromGlobals();
    }
}
